<template>
  <div class="login-page">
    <el-card class="el-card">
      <template #header>黑马面经运营后台</template>

      <el-form :model="form" :rules="rules" auto-complete="off" ref="form">
        <el-form-item label="用户名：" prop="username" >
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="密码：" prop="password">
          <el-input v-model="form.password" placeholder="输入用户密码：" type="password" />
        </el-form-item>

        <el-form-item class="login-btn">
          <el-button type="primary" @click="submitForm">登录</el-button>

          <el-button @click="resetForm">重置</el-button>

        </el-form-item>

      </el-form>

    </el-card>

  </div>

</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 11, message: '长度在 5 到 11 个字符', trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,11}$/, message: '请输入 5 到 10 位的密码', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    submitForm () {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          const res = await login(this.form)
          setToken(res.token)
          this.$router.push('/')
        }
      })
    },
    resetForm () {
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page{
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('../../assets/login-bg.svg');
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;

  .el-card {
  width: 420px;
  margin: 0 auto;
  // 深度作用选择器   ::v-deep   /deep/
  ::v-deep .el-card__header {
    background: rgba(114,124,245,1);
    text-align: center;
    color: white;
    height: 80px;
    line-height: 40px;
    font-size: 18px;
  }
  }
  .el-form {
    padding: 0 20px;
  }
  .login-btn {
    text-align: center;
  }
}

</style>
